import axios from "axios";
const MapLogic = {
  data() {
    return {
      gridData: [],
      dialogFormVisible: false,
      localDataBase: [],
    };
  },
  computed: {
    collected() {
      let temp = [];
      this.localDataBase.forEach((item) => {
        item.collectStatus === 2 ? temp.push(item.landId) : "";
      });
      return temp;
    },
    uncollected() {
      let temp = [];
      this.localDataBase.forEach((item) => {
        item.collectStatus === 1 ? temp.push(item.landId) : "";
      });
      return temp;
    },
    Allcollected() {
      let temp = [];
      this.localDataBase.forEach((item) => {
        temp.push(item.landId)
      });
      return temp;
    }
  },
  methods: {
    getMyAll(collectStatus) {
      let currentTask;
      if (localStorage.getItem("currentTask/setCurrentTask")) {
        currentTask = JSON.parse(
          localStorage.getItem("currentTask/setCurrentTask")
        );
        if (collectStatus === '已采集') {
          collectStatus = 2;
        } else if (collectStatus === '未采集') {
          collectStatus = 1;
        } else {
          collectStatus = 0;
        }
        let params;
        if (collectStatus === 0) {
          params = {
            'collectTaskLand.planId': currentTask.data.id,
          }
        } else {
          params = {
            'collectTaskLand.planId': currentTask.data.id,
            'collectTaskLand.collectStatus': collectStatus
          }
        }
  
        return this.$axios
          .get("/rbac/data/collectTaskLand/listPart", {
            params
          })
          .then((res) => {
            console.log(res.data.list)
            this.localDataBase = res.data.list || [];
            return Promise.resolve(this.localDataBase);
          });
      } else {
        this.$message({
          type: "warning",
          message: "当前没有任何可选择的任务，需要管理员先创建任务",
        });
        return Promise.resolve(this.localDataBase);
      }
    },
    setFilterProperty(val) {
      const loading = this.$loading({
        lock: true,
        text: '正在搜索图斑数据,请耐心等待',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      setTimeout(() => {
        this.getMyAll(val.value)
          .then((res) => {
            if (res instanceof Array && res.length) {
              this.$set(this.filterObj, val.title, val.value);
              this.mapFilter();
            } else {
              this.$message({
                type: "warning",
                message: "当前可能没有任务或需要您手动指定",
              });
            }
          })
        loading.close();
      }, 5000)
    },
    setLevel(val) {
      this.levelValue = val;
      this.mapFilter();
    },
    //图斑筛选 根据filterObj过滤图斑
    mapFilter() {
      let val = this.filterObj;
      let fil = [];
      // 镇街筛选
      val.townsValue != "全部" ? fil.push(["==", "XZMC", val.townsValue]) : "";
      //村居筛选

      if (val.streetsValue instanceof Array) {
        val.streetsValue != "全部"
          ? fil.push(["in", "村居", ...val.streetsValue])
          : "";
      } else {
        val.streetsValue != "全部"
          ? fil.push(["==", "村居", val.streetsValue])
          : "";
      }

      // 已采集
      val.figureTypeValue == "已采集"
        ? fil.push(["in", "图斑ID", ...this.collected])
        : "";
      //未采集
      val.figureTypeValue == "未采集"
        ? fil.push(["in", "图斑ID", ...this.uncollected])
        : "";
      //全部
      val.figureTypeValue == "全部"
        ? fil.push(["in", "图斑ID", ...this.Allcollected])
        : "";

      //地块登记过滤
      this.levelValue && this.levelValue.length
        ? fil.push(["in", "二级类型", ...this.levelValue])
        : "";

      console.log(fil)
      //设置过滤器
      this.map.setFilter("experimentalData-line-layer", ["all", ...fil]);
      this.map.setFilter("experimentalData-area-layer", ["all", ...fil]);
    },
    createArea(e) {
      //当前多边形的坐标点集合
      let current = e.features[0].geometry.coordinates[0];
      let scopeList = [];
      current.forEach((item) => {
        scopeList.push({
          latitude: item[0],
          longitude: item[1],
        });
      });
      let json = JSON.stringify({
        scopeList: scopeList,
      });
      console.log(json);
      //获取被框选的图斑
      let _this = this;
      axios({
        url: "https://api.leadinsight.cn/tianyan/api/v1/spot/spotCode",
        method: "post",
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
        },
        data: json,
      }).then((res) => {
        let data = res.data.data
        if (data.length > 50) {
          _this.$message({
            message: `框选工具最多只能选择50个图斑`,
            type: "warning",
          });
          return false
        } else if (data.length === 0) {
          _this.$message({
            message: `您未选择任何图斑`,
            type: "warning",
          });
          return false
        } else {
          let landID = res.data.data
          // landID = landID.replace(/\"/ig, "")
          let landPlanId = '';
          if (localStorage.getItem('currentTask/setCurrentTask') && this.$route.name === 'Map') {
            let currentTask = JSON.parse(localStorage.getItem('currentTask/setCurrentTask'))
            landPlanId = currentTask.data.id;
          } else {
            _this.$message({
              message: `您必须选择一个任务`,
              type: "warning",
            });
            return false
          }
          this.$axios.get('rbac/data/CollectTaskLand/list', {
            params: {
              landCodes: landID.join(','),
              "collectTaskLand.planId": landPlanId
            }
          }).then((res) => {
            _this.mapList = res.data.list
            _this.toggleSelection(res.data.list)
            _this.landCodeArray = []
            _this.mapListdialogTableVisible = true
          })
        }
      });
    },
    panToTown(value) {
      switch (value) {
        case "里水镇":
          this.map.panTo([113.136793, 23.227369]);
          this.center = [113.136793, 23.227369];
          break;
        case "狮山镇":
          this.map.panTo([113.012359, 23.160829]);
          this.center = [113.012359, 23.160829];
          break;
        case "大沥镇":
          this.map.panTo([113.121615, 23.114786]);
          this.center = [113.121615, 23.114786];
          break;
        ///////////防止眼花/////////
        case "罗村街道":
          this.map.panTo([113.04255, 23.063944]);
          this.center = [113.04255, 23.063944];
          break;
        case "丹灶镇":
          this.map.panTo([112.921315, 23.059877]);
          this.center = [112.921315, 23.059877];
          break;
        case "桂城街道":
          this.map.panTo([113.183875, 23.03225]);
          this.center = [113.183875, 23.03225];
          break;
        ///////////防止眼花/////////
        case "西樵镇":
          this.map.panTo([112.94087, 22.93736]);
          this.center = [112.94087, 22.93736];
          break;
        case "南樵镇":
          this.map.panTo([112.872017, 22.861839]);
          this.center = [112.872017, 22.861839];
          break;
        case "九江镇":
          this.map.panTo([112.993321, 22.856176]);
          this.center = [112.993321, 22.856176];
          break;
        case "全部":
          this.center = [113.02151029197415, 22.900558123212093];
          break;
      }
    },
  }
};
export default MapLogic;
